import com.mysql.jdbc.Connection;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yinsh
 * Date: 2025-03-08
 * Time: 18:51
 */
public class Update {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        int id = 0;
        String name = null;
        System.out.println("请选择要修改的数据：");
        System.out.println("0.    ID        ");
        System.out.println("1.   NAME       ");
        int choice = scanner.nextInt();
        if (choice == 0) {
            id = scanner.nextInt();
        }else {
            name = scanner.nextLine();
        }

        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql//127.0.0.1:3306/java1?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("19705004009");
        Connection connection = (Connection) dataSource.getConnection();

        if (choice==0) {
            String sql = "update student set id=? where id=?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            preparedStatement.setString(2,name);
        } else {

        }
    }
}
